package com.dhj.mall.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.dhj.mall.bean.Msg;
import com.dhj.mall.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class AnalysisController {
	
	@Autowired
	AnalysisService analysisService;
	
	/**
	 *地域分析 
	 * @param pro
	 * @param area
	 * @return
	 */
	@RequestMapping(value = "/analysis/regional_analysis", method = RequestMethod.GET)
	@ResponseBody
	public Msg getRegionalAnalysis(@RequestParam("pro") String pro, @RequestParam("area") String area) {

			Map<String, Integer> map = analysisService.getRegionalAnalysis(pro,area);
			return Msg.success().add("map", map);

	}
	
	
	/**
	 *等级分析 
	 */
	@RequestMapping(value = "/analysis/grade_analysis", method = RequestMethod.GET)
	@ResponseBody
	public Msg getGradeAnalysis() {

			Map<String, List<Integer>> map = analysisService.getGradeAnalysis();
			return Msg.success().add("map", map);

	}
	
	
	/**
	 *会员群体分析 
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/analysis/group_analysis", method = RequestMethod.GET)
	@ResponseBody
	public Msg getGroupAnalysiss() throws ParseException {
		Map<String, List<Integer>> map =  new HashMap<String, List<Integer>>();
		for (int i = 0; i < 7; i++) {
			List<Integer> list = analysisService.getGroupAnalysiss(getDateGroup()[i]);
			
			map.put(getDateGroupSplit(getDateGroup())[i], list);
		} 
			
			return Msg.success().add("map", map).add("date",getDateGroupSplit(getDateGroup()));

	}
	
	
	public String[] getDateGroupSplit(String[] strings) {
		String[] strins = new String[7];
		// 最多分割出3个字符串
		for (int i = 0; i < strings.length; i++) {
			 String[] sourceStrArray = strings[i].split("-");
			 // 最多分割出3个字符串
	        sourceStrArray = strings[i].split("-", 2);
			strins[i] = sourceStrArray[1];
		}
		return strins;

	}
	

	//格式化date类型为string类型
	public String[] getDateGroup() {
		String[] strings = new String[7];
		Date dNow = new Date(); // 当前时间
		strings[0] = new SimpleDateFormat("yyyy-MM-dd").format(dNow);
		strings[1] = new SimpleDateFormat("yyyy-MM-dd").format(getDate(dNow, -1));
		strings[2] = new SimpleDateFormat("yyyy-MM-dd").format(getDate(dNow, -2));
		strings[3] = new SimpleDateFormat("yyyy-MM-dd").format(getDate(dNow, -3));
		strings[4] = new SimpleDateFormat("yyyy-MM-dd").format(getDate(dNow, -4));
		strings[5] = new SimpleDateFormat("yyyy-MM-dd").format(getDate(dNow, -5));
		strings[6] = new SimpleDateFormat("yyyy-MM-dd").format(getDate(dNow, -6));
		return strings;

	}
	
	

	//得到前七天的时间
	public Date getDate(Date date, int ff) {
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(date);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, ff); // 设置为前几天
		dBefore = calendar.getTime(); // 得到前几天的时间
		return dBefore;
	}
	
	

}
